package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.PageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.category.DishFlavorMapper;
import com.sky.mapper.category.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品口味
     */
    public void saveDishFlavor(List<DishFlavor> flavors ,Long dishId){
        //存菜品口味
        //判断菜品口味是否为空
        if(!CollectionUtils.isEmpty(flavors)){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.add(flavors);
        }
    }


    /**
     * 新增菜品
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DishDTO dto) {
        //拷贝对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        //补全状态
        dish.setStatus(StatusConstant.DISABLE);
        //调用mapper
        dishMapper.add(dish);
        //新增菜品口味
        saveDishFlavor(dto.getFlavors(),dish.getId());
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        dto.check();
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除菜品
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return;
        }
        //判断菜品是否为在售状态
        List<Dish> dishes = dishMapper.queryOnsaleDishByIds(ids);
        log.info("查询到的在售状态菜品为:{}",dishes);
        if(!CollectionUtils.isEmpty(dishes)){
            throw new BaseException("存在启售状态菜品,删除失败");
        }
        //删除菜品表
        dishMapper.deleteByIds(ids);
        //删除菜品口味表
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO queryById(Long id) {
        //查询菜品信息
        DishVO dishVO = dishMapper.queryById(id);
        dishVO.setFlavors(dishFlavorMapper.queryByDishId(id));
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dto
     */
    @Override
    public void update(DishDTO dto) {
        //拷贝对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);
        //先根据菜品id将菜品口味id清空之后根据实际情况重新插入
        dishFlavorMapper.deleteByDishIds(List.of(dto.getId()));
        //判断菜品口味是否为空
        if(CollectionUtils.isEmpty(dto.getFlavors())){
            return;
        }
        saveDishFlavor(dto.getFlavors(),dto.getId());
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> queryAllByCategoryId(Integer categoryId) {
        return dishMapper.queryAllByCategoryId(categoryId);
    }


}
